package com.chuangjiangx.member.manager.client.web.basic.controller;

import com.chuangjiangx.commons.response.CamelResponse;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.basic.ddd.query.MbrCountQuery;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.client.web.stored.request.OrderCountRequest;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.stored.ddd.dal.condition.AppOrderCountCondition;
import com.chuangjiangx.member.stored.ddd.dal.dto.AppOrderStreamCount;
import com.chuangjiangx.member.stored.ddd.dal.dto.AppOrderStreamDetail;
import com.chuangjiangx.member.stored.ddd.query.dto.AppOrderCount;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cj-ch
 * @date 2018/6/19 下午5:10
 */
@RestController
@Api(tags = "消费" , value = "会员消费记录")
@RequestMapping(value = "/mbr/count",produces = MediaType.APPLICATION_JSON_VALUE)
public class MbrCountClientController {
    @Autowired
    private MbrCountQuery mbrCountQuery;



    /**
     * 订单统计
     * 统计 扣  退
     */
    @RequestMapping(value = "/order-count",method = {RequestMethod.GET,RequestMethod.POST})
    @Login
    @ApiOperation(value = "订单统计(统计，扣退)")
    public CamelResponse<AppOrderCount> orderCount(OrderCountRequest request){
        ThreadLocalUser token = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        AppOrderCountCondition condition = new AppOrderCountCondition();
        condition.setMerchantId(token.getMerchantId());
        condition.setStart(request.getStart());
        condition.setEnd(request.getEnd());
        condition.setStoreId(token.getStoreId());
        AppOrderStreamCount count = mbrCountQuery.appOrderStreamCount(condition);
        AppOrderCount appOrderCount = new AppOrderCount();
        appOrderCount.setTotalRefund(count.getMbrOrderRefundSum());
        appOrderCount.setTotalDeduct(count.getMbrOrderSum());
        return (CamelResponse<AppOrderCount>) ResponseUtils.successCamel(appOrderCount);
    }

    /**
     * 会员消费记录-打印流水
     * (弹出窗口统计:流水金额/流水笔数)
     *
     */
    @RequestMapping(value = "/order-stream-count",method = {RequestMethod.GET,RequestMethod.POST})
    @Login
    @ApiOperation(value = "会员消费记录-打印流水(弹出窗口统计:流水金额/流水笔数)")
    public CamelResponse<AppOrderStreamCount> orderStreamCount(OrderCountRequest request){
        ThreadLocalUser token = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        AppOrderCountCondition condition = new AppOrderCountCondition();
        condition.setMerchantId(token.getMerchantId());
        condition.setStart(request.getStart());
        condition.setEnd(request.getEnd());
        condition.setStoreId(token.getStoreId());
        condition.setStoreUserId(token.getStoreUserId());


        AppOrderStreamCount count = mbrCountQuery.appOrderStreamCount(condition);
        return (CamelResponse<AppOrderStreamCount>) ResponseUtils.successCamel(count);
    }

    /**
     * 会员消费记录-打印流水详情
     * (打印消费明细/消费统计)
     */
    @RequestMapping(value = "/order-stream-detail",method = {RequestMethod.GET,RequestMethod.POST})
    @Login
    @ApiOperation(value = "会员消费记录-打印流水详情(打印消费明细/消费统计))")
    public CamelResponse<List<AppOrderStreamDetail>> orderStreamDetail(OrderCountRequest request){
        ThreadLocalUser token = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        AppOrderCountCondition condition = new AppOrderCountCondition();
        condition.setMerchantId(token.getMerchantId());
        condition.setStart(request.getStart());
        condition.setEnd(request.getEnd());

        if(1 == token.getUserType()){
            condition.setStoreId(token.getStoreId());
        } else if(2 == token.getUserType()){
            condition.setStoreUserId(token.getStoreUserId());
        }

        List<AppOrderStreamDetail> streamDetails = mbrCountQuery.appOrderStreamDetails(condition);
        return (CamelResponse<List<AppOrderStreamDetail>>) ResponseUtils.successCamel(streamDetails);
    }



}
